package com.rongshu.view.discovery.fragment;

import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.utils.Constant;
import com.rongshu.view.base.BaseFragment;
import com.rongshu.view.base.mvp.INetView;
import com.rongshu.view.discovery.adapter.OtherArticleAdapter;
import com.rongshu.view.discovery.bean.ArticleListItemBean;
import com.rongshu.view.discovery.bean.ArticleListResponse;
import com.rongshu.view.discovery.presenter.UserArticlePresenter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * created by zhengweis on 2019/1/16.
 * description：
 */
public class TaArticleFragment extends BaseFragment<UserArticlePresenter> implements INetView {
    @BindView(R.id.head_middle)
    TextView headMiddle;
    @BindView(R.id.empty_hint)
    TextView emptyHint;
    @BindView(R.id.layout_empty)
    RelativeLayout layoutEmpty;
    @BindView(R.id.article_list)
    RecyclerView articleList;
    @BindView(R.id.refresh_view)
    SmartRefreshLayout refreshView;
    @BindView(R.id.empty_img)
    ImageView emptyImg;
    private int pageIndex = 1;
    private int pageSize = 10;
    private int userId;
    private OtherArticleAdapter adapter;
    private List<ArticleListItemBean> collectedArticles;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_user_article;
    }

    @Override
    protected void initViews() {
        Intent intent = getActivity().getIntent();
        if (intent != null) {
            userId = intent.getIntExtra(Constant.USER_ID, 0);
        }
        headMiddle.setText("Ta的文章");
        emptyHint.setText("Ta还没有文章哦～");
        emptyImg.setBackground(ContextCompat.getDrawable(getActivity(), R.drawable.empty_article));
        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        articleList.setLayoutManager(manager);
        collectedArticles = new ArrayList<>();
        adapter = new OtherArticleAdapter(getActivity(), collectedArticles);
        articleList.setAdapter(adapter);
    }

    @Override
    protected void initData() {
        setPresenter(new UserArticlePresenter(getActivity()), this);
        refreshView.autoRefresh();
        refreshView.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refreshlayout.resetNoMoreData();
                pageIndex = 1;
                presenter.getArticleList(userId, pageIndex, pageSize);
            }
        });
        refreshView.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                pageIndex++;
                presenter.getArticleList(userId, pageIndex, pageSize);
            }
        });
    }

    @Override
    protected void updateViews(boolean isRefresh) {

    }


    @OnClick(R.id.head_left)
    public void onViewClicked() {
        getActivity().finish();
    }

    @Override
    public void success(Object object, int tag) {
        ArticleListResponse response = (ArticleListResponse) object;
        if (response != null && response.getResponse() != null) {
            List<ArticleListItemBean> list = response.getResponse();
            if (pageIndex == 1) {
                collectedArticles.clear();
            }
            if (refreshView.isLoading()) {
                refreshView.finishLoadmore();
            }
            if (refreshView.isRefreshing()) {
                refreshView.finishRefresh();
            }
            collectedArticles.addAll(list);
            adapter.notifyDataSetChanged();
            if (collectedArticles == null || collectedArticles.size() == 0) {
                refreshView.setVisibility(View.GONE);
                layoutEmpty.setVisibility(View.VISIBLE);
            }
            if (list.size() < pageSize) {
                refreshView.setLoadmoreFinished(true);
            }
        }
    }

    @Override
    public void faild(Object object, int tag) {

    }
}
